import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'
import Layout from '../Layout/index.vue'
import store from '../store'

const routes:RouteRecordRaw[] = [
    {
        path : '/login',
        name : 'login',
        component : () => import('@/view/login/index.vue')
    },

    {
        path : '/',
        component : Layout,
        meta : {title : '登录'},
        children : [
            {
                path : '',
                component : () => import('@/view/home/index.vue')
            }
        ]
    }
]
const router = createRouter({
    history: createWebHashHistory(),
    routes
})

let init = false

router.beforeEach((to, from, next) => {
    if(to.name !== 'login') {
        if(!init) {
            if(store.getters.accessExpireStatus) { //登录有效
                init = true
                store.dispatch('login/deliveryRefreshTask')
                store.dispatch('getPermissionResource').then(() => {
                    next({path : to.path})
                }) //获取权限
                return
            }else { //登录无效，跳去登录
                next({path : '/login'})
            }
            return
        }
    }

    next()

})

export default router